<?php if(!defined('MS_XTIGER')) exit('Access Denied');
/**
 * Created by openXtiger.org.
 * User: xtiger
 * Date: 2009-6-15
 * Time: 11:15:55
 */

/*public */function settingmodel_replace($var, $value='') {
    $db = xt_load('db');
    $db->query('REPLACE INTO '.xt_tname('settings')." (variable, value) VALUES ('$var', '$value')");       
}
/*public */function settingmodel_delete($var) {
    $db = xt_load('db');
    $db->query('DELETE FROM '.xt_tname('settings')."  WHERE variable='$var'");
}
/*public */function settingmodel_variable($var) {
    $db = xt_load('db');
    $query = $db->query('SELECT value FROM '.xt_tname('settings')."  WHERE variable='$var'");
    return $db->result($query, 0);
}
/*public */function settingmodel_updatecache() {
    $db = xt_load('db');
    $_XSC = array();

	$query = $db->query('SELECT variable,value FROM '.xt_tname('settings'));
	while ($set = $db->fetch_array($query)) {
		$_XSC[$set['variable']] = $set['value'];
	}
	$cachefile = MS_CACHEPATH.'./RuntimeSetting.cache.php';
	xt_writecache($cachefile,$_XSC);
}
/*public */function settingmodel_admincustom($title, $url, $uid, $sort = 0) {
    $db = xt_load('db');
    $tbl = xt_tname('admincustom');
	$id = $db->result_first("SELECT id FROM $tbl WHERE uid='$uid' AND sort='$sort' AND url='$url'");
	if($id) {
		$db->query("UPDATE $tbl SET title='$title', clicks=clicks+1, dateline='".XT_TIME."' WHERE id='$id'");
	} else {
		$db->query("INSERT INTO $tbl(title, url, sort, uid, dateline) VALUES ('$title', '$url', '$sort', '$uid', '".XT_TIME."')");
	}
}
/*public */function settingmodel_historymenus($uid) {
    $db = xt_load('db');
    $tbl = xt_tname('admincustom');
	$query = $db->query("SELECT title, url FROM $tbl WHERE uid='$uid' AND sort='0' ORDER BY dateline DESC LIMIT 0, 10");
    $historymenus = array();
    while($custom = $db->fetch_array($query)) {
        $historymenus[] = array($custom['title'], $custom['url']);
    }
    return $historymenus;
}